package com.lt.tccdemo.service;

import com.lt.tccdemo.db108.dao.SendMessageMapper;
import com.lt.tccdemo.db108.model.SendMessage;
import com.lt.tccdemo.db108.model.SendMessageExample;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lucine
 * @version 1.0 2021/10/5 18:16
 */
@Service
public class OrderScheduler {

    @Autowired
    private SendMessageMapper sendMessageMapper;

    @Autowired
    private OrderService orderService;

    @Scheduled(cron = "0/10 * * * * ?")
    public void orderNotify() throws Exception {
        System.out.println("进入orderNotify");
        SendMessageExample sendMessageExample = new SendMessageExample();
        //未发送
        sendMessageExample.createCriteria().andStatusEqualTo(0);
        //查询出所有需要发送的消息
        List<SendMessage> sendMessages = sendMessageMapper.selectByExample(sendMessageExample);
        if(sendMessages==null || sendMessages.size() ==0){
            return;
        }
        for(SendMessage sendMessage:sendMessages){
            Integer orderId = sendMessage.getOrderId();
            System.out.println(orderId);
            //模拟调用处理订单
            CloseableHttpClient HttpClient = HttpClientBuilder.create().build();
            HttpPost httpPost = new HttpPost("http://localhost:8080/handleOrder");
            NameValuePair nameValuePair = new BasicNameValuePair("orderId",orderId+"" );
            List<NameValuePair> NameValuePairList = new ArrayList<>();
            NameValuePairList.add(nameValuePair);
            HttpEntity urlEncodedFormEntity = new UrlEncodedFormEntity(NameValuePairList);
            httpPost.setEntity(urlEncodedFormEntity);
            CloseableHttpResponse response = HttpClient.execute(httpPost);
            String s = EntityUtils.toString(response.getEntity());
            System.out.println(s);
            if("success".equals(s)){
                //发送成功
                sendMessage.setStatus(1);
            }else {
                Integer falureCnt = sendMessage.getFalureCnt();
                falureCnt++;
                if(falureCnt > 5){
                    //如果重试大于5次就更改为失败
                    sendMessage.setStatus(2);
                }
                sendMessage.setFalureCnt(falureCnt);
            }
            sendMessage.setUpdateUser(0);
            sendMessage.setUpdateTime(new Date());
            sendMessageMapper.updateByPrimaryKey(sendMessage);
        }


    }

}
